package item.inkcloud.userservice.controller;


import com.alibaba.fastjson.JSONObject;
import item.inkcloud.userservice.entity.UserInfo;
import item.inkcloud.userservice.feignService.JwtService;
import item.inkcloud.userservice.pojo.CookieTemplate;
import item.inkcloud.userservice.pojo.ProvinceHandler;
import item.inkcloud.userservice.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;



@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    ProvinceHandler provinceHandler;

    @Autowired
    CookieTemplate cookieTemplate;

    @Autowired
    JwtService jwtService;

    @Autowired
    RegistUserService registUserService;

    @Autowired
    LoginUserService loginUserService;

    @Autowired
    GetUserInfoService getUserInfoService;

    @Autowired
    UpdateUserInfoService updateUserInfoService;

    @Autowired
    ChangePwdService changePwdService;

    @Autowired
    ChangeProvinceService changeProvinceService;

    @RequestMapping("/register")
    Map regist(@RequestBody JSONObject jsonParam){

        UserInfo info = jsonParam.toJavaObject(UserInfo.class);

        HashMap<String,Object> resultMap = new HashMap<String, Object>();

        int status = registUserService.registUser(info);

        resultMap.put("status",status);

        return resultMap;
    }

    @RequestMapping(value = "/login")
    Map login(@RequestBody JSONObject jsonParam, HttpServletResponse response){

        HashMap<String,Object> resultMap = new HashMap<String, Object>();

        UserInfo info=jsonParam.toJavaObject(UserInfo.class);

        int status = loginUserService.login(info.getUserid(),info.getPassword());

        resultMap.put("status",status);

        if (status == 1) {
            String token = null;
            String userid = info.getUserid();
            token = jwtService.getToken(userid);
            if(token != null){
                Cookie useridCookie = cookieTemplate.mkCookie("userid", userid);
                Cookie tokenCookie = cookieTemplate.mkCookie("token",token);
                response.addCookie(useridCookie);
                response.addCookie(tokenCookie);
            }

        }

        return resultMap;
    }




    @RequestMapping("/userinfo")
    HashMap getUserInfo(@RequestParam("userid") String userid){



       HashMap<String,Object> resultMap = new HashMap<>();


        resultMap = getUserInfoService.getInfo( userid);


        return resultMap;


    }


    @RequestMapping("/update")
    HashMap updateUser(@RequestBody JSONObject jsonPara,@RequestParam("userid") String userid){

        UserInfo info = jsonPara.toJavaObject(UserInfo.class);

        info.setUserid(userid);

        HashMap<String,Object> resultMap = new HashMap<>();


        int status = updateUserInfoService.update(info);



        resultMap.put("status",status);

        return resultMap;
    }


    @RequestMapping("/changePwd")
    HashMap changePwd(@RequestBody HashMap<String,String> paraMap,@RequestParam("userid") String userid){

        HashMap<String,Object> resultMap = new HashMap<>();


        String oldPassword = paraMap.get("oldPassword");
        String newPassword = paraMap.get("newPassword");


        int status = changePwdService.changePwd(userid,oldPassword,newPassword);



        resultMap.put("status",status);

        return resultMap;
    }

    @RequestMapping("/changeProvince")
    HashMap changeProvince(@RequestBody JSONObject jsonpara,HttpServletResponse response,@RequestParam("userid") String userid){
        HashMap<String,Object> resultMap = new HashMap<>();



        Object province = jsonpara.get("province");


        int status = changeProvinceService.changeProvince( userid,(String) province);

        province = provinceHandler.handleProvince((String) province);

            resultMap.put("status",status);

            if(status==1){
                Cookie cookie = cookieTemplate.mkCookie("province", (String) province);
                response.addCookie(cookie);
            }


            return resultMap;

    }


    @RequestMapping("/getUsername")
    String getUsernameById(@RequestParam("userid") String userid){


        HashMap info = getUserInfoService.getInfo(userid);


        HashMap infoData = (HashMap<String,String>) info.get("data");

        String username =(String) infoData.get("username");


        return  username;

    }

}
